<?php
require_once "backend/Config.php";
require_once "HTMLable.php";
require_once "Menu.php";

class Template extends HTMLable {
	protected $content, $loggedIn, $prefix, $user;
	
	public function __construct($content = null) {
		session_start();
		$this->loggedIn = false;
		if ($_SESSION['user'] !== null)
			$this->loggedIn = true;
		
		if ($content != null)
			$this->content = $content;
		else
			$content = "";
		
		$config = new Config();
		$this->prefix = $config->c['dirs']['base_rel'];
		
		$this->genHTML();
	}
	
	public function genHTML() {
		$this->html  = $this->loadFile("include/html/head.html");
		
		$menu = new Menu($this->loggedIn, $this->prefix);
		$this->html .= $menu->getHTML();
		
		$this->html .= $this->loadFile("include/html/form_search.html");
		
		if ($this->user != null) {
			$this->user->genHTML();
			$this->html .= $this->user->getHTML();
		}
		
		$this->html .= "
			<div id=\"content\">\n".
			$this->content
			."</div>";
		
		$this->html .= $this->loadFile("include/html/tail.html");
	}
	
	public function addContent($content) {
		$this->content .= $content;
	}
	
	public function addContentFromFile($file) {
		$this->content .= $this->loadFile($file);
	}
	
	public function resetContent() {
		$this->content = "";
	}
	
	public function setUser($user) {
		$this->user = $user;
		$this->genHTML();
	}
	
	private function loadFile($file) {
		$handle = fopen($file, "r");
		$text = fread($handle, filesize($file));
		fclose($handle);
		return $text;
	}
}
?>
